#!/usr/bin/env python

import os

from kivy.metrics import dp
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.uix.popup import Popup
from kivy.uix.tabbedpanel import TabbedPanel, TabbedPanelHeader

from amgineirc.view.settings.settingsView import SettingsView
from amgineirc.view.channels.connectionsView import ConnectionsView
from amgineirc.viewModel.appViewModel import AppViewModel

class AppView(FloatLayout):
    connected_string = 'Connected'
    not_connected_string = 'Not Connected'
    connected_fg = [0.0, 1.0, 0.0, 1.0] # green
    not_connected_fg = [1.0, 0.0, 0.0, 1.0] # red

    def __init__(self, appdata):
        FloatLayout.__init__(self, size_hint=(1.0, 1.0))
        # upper view
        self.upper_view = _AppUpperView(appdata,
                                        tab_height=dp(60),
                                        tab_width=dp(250),
                                        do_default_tab=True,
                                        default_tab_text='AmgineIRC',
                                        default_tab_content = self._build_about_app_content(appdata),
                                        size_hint=(1.0, 0.9),
                                        pos_hint={'x':0.0, 'y':0.1})
        self.add_widget(self.upper_view)
        # lower view
        self._lower_view = _AppLowerView(appdata,
                                         size_hint=(1.0, 0.1),
                                         pos_hint={'x':0.0, 'y':0.0})
        self.add_widget(self._lower_view)
        self.set_status_bar(False)
        AppViewModel(self, appdata)

    def _build_about_app_content(self, appdata):
        # image
        src = os.path.join(appdata['app'].directory, 'amgineirc_IRC_dessert.png')
        image = Image(source=src)
        # text (name and version)
        label = Label(text='', markup=True)
        sizei = label.font_size * 2
        sizes = '[size=%i]' % (sizei)
        letters = [sizes,
                   '[color=b08c62]A[/color]',
                   '[color=b08c62]m[/color]',
                   '[color=b08c62]g[/color]',
                   '[color=b08c62]i[/color]',
                   '[color=b08c62]n[/color]',
                   '[color=b08c62]e[/color]',
                   '[color=82c477]I[/color]',
                   '[color=82c477]R[/color]',
                   '[color=82c477]C[/color]',
                   '[/size]']
        name = ''.join(letters)
        version = 'Version %i.%i.%i' % (appdata['version']['major'],
                                         appdata['version']['minor'],
                                         appdata['version']['bugfix'])
        text = '%s %s' % (name, version)
        label.text = text
        layout = GridLayout(cols=1, padding=20)
        layout.add_widget(image)
        layout.add_widget(label)
        return layout
        

    def set_status_bar(self, connected):
        if connected == True:
            self._lower_view.text=AppView.connected_string
            self._lower_view.color=AppView.connected_fg
        else:
            self._lower_view.text=AppView.not_connected_string
            self._lower_view.color=AppView.not_connected_fg

class _AppLowerView(Label):
    def __init__(self, appdata, **kargs):
        kargs['text'] = u''
        Label.__init__(self, **kargs)


class _AppUpperView(TabbedPanel):
    """this class does not have a view model
    its pages have their own view models
    """

    settings_page_label = 'Configuration'
    connections_page_label = 'Connections'

    def __init__(self, appdata, **kargs):
        TabbedPanel.__init__(self, **kargs)
        # add the settings tab
        self._settings_th = TabbedPanelHeader(text=_AppUpperView.settings_page_label)
        self._settings_th.content = SettingsView(appdata,
                                                 tab_width=dp(200))
        self.add_widget(self._settings_th)
        # add the connections tab
        self._connections_th = TabbedPanelHeader(text=_AppUpperView.connections_page_label)
        self.connections_view = ConnectionsView(appdata,
                                                tab_height=40,
                                                tab_width=200)
        self._connections_th.content = self.connections_view
        self.add_widget(self._connections_th)

    def raise_connections_page(self):
        self.switch_to(self._connections_th)
